# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023/11/5
desc: 
sample: 
"""
import csv
import os

import openpyxl


def get_data_path(file):
    full_path = os.path.join(os.path.dirname(__file__), 'data', file)
    return full_path

#[[] ,[],[]]
def getcsvList(file, header=True):
    data_list = []
    with open(get_data_path(file), encoding='utf-8') as f:
        reader = csv.reader(f)
        if header:
            next(reader)
        for item in reader:
            data_list.append(item)
    return data_list

#[{} ,{}]
def getcsvListDict(file):
    data_list = []
    with open(get_data_path(file), encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for item in reader:
            data_list.append(item)
    return data_list

#[[] ,[],[]]
def getExcelList(file,sheet_index=1,header=True):
    data_list = []
    workbook = openpyxl.load_workbook(get_data_path(file))
    worksheet = workbook.worksheets[sheet_index-1]
    if header:
       rows= worksheet.iter_rows(min_row=2)
    else:
        rows= worksheet.iter_rows()
    for row in rows:
        cell_list=[]
        for row in row:
            cell_list.append(row.value)
        data_list.append(cell_list)
    return data_list

#[{} ,{},{}]
def getExcelDict(file,sheet_index=0):
    data_list = []
    workbook = openpyxl.load_workbook(get_data_path(file))
    worksheet = workbook.worksheets[sheet_index]
    rows = worksheet.iter_rows(min_row=2)
    for row in rows:
        cell_dict={}
        for row_item in row:
            cell = worksheet.cell(row=1, column=row_item.column)
            cell_dict[cell.value]= row_item.value
        data_list.append(cell_dict)
    return data_list


if __name__ == '__main__':
    #print(getcsvList('id2.csv',header=False))
    #print(getcsvListDict('id.csv'))
    print(getExcelDict('case.xlsx'))
